/**
 * 系统权限管理-用户管理调用接口
 */
import { URL_PREFIX_COMPATIBLE } from '@/configs';
import type { PageResponse, Response } from 'umi';
import { post, request } from 'umi';

import type { IRoleItem, IUserDetail, IUserItem } from '@/types/permission/user';

/**
 * 用户列表
 */
export async function queryUserList(data: {
  userAppCode: string;
  appCode: string;
  userName?: string;
  deptCode?: string;
  mobile?: string;
}) {
  return post<PageResponse<IUserItem>>('/user/account/query/dept/user', data,);
}

/**
 * 角色列表
 */
export async function queryRoleList(data: {
  current?: number;
  pageSize?: number;
  appCode: string;
  /**0禁用，1启用 */
  roleStatus?: '0' | '1';
  /** 当前访问系统编码 */
  userAppCode: string;
}) {
  return post<Response<IRoleItem[]>>('/user/account/get/role/list', data,);
}

/**
 * 角色列表，受授权组影响
 */
export async function queryRoleListAuthed(data: {
  current?: number;
  pageSize?: number;
  appCode: string;
  /**0禁用，1启用 */
  roleStatus?: '0' | '1';
  /** 当前访问系统编码 */
  userAppCode: string;
}) {
  return post<Response<IRoleItem[]>>('/user/account/get/perm/role/list', data,);
}

/**
 * 通过手机号查询用户详情信息
 * @param data
 * @returns
 */
export async function getUserByMobile(data: {
  mobile: number | undefined;
  appCode: string;
  userAppCode: string;
}) {
  return request<Response<IUserDetail>>('/user/account/get/user/by/mobile', {
    method: 'post',
    data,
    prefix: URL_PREFIX_COMPATIBLE,
  });
}

/**
 * 新增用户授权
 * @param data
 * @returns
 */
export async function addAuth(
  data: IUserDetail & {
    appCode: string;
    userAppCode: string;
  },
) {
  return request<Response<boolean>>('/user/account/add/user', {
    method: 'post',
    data,
    prefix: URL_PREFIX_COMPATIBLE,
  });
}

/**
 * 更新用户授权
 * @param data
 * @returns
 */
export async function updateAuth(
  data: IUserDetail & {
    appCode: string;
    /** 当前访问系统标识 */
    userAppCode: string;
  },
) {
  return request<Response<boolean>>('/user/account/update/user', {
    method: 'post',
    data,
    prefix: URL_PREFIX_COMPATIBLE,
  });
}

/**
 * 用户详情
 * @param data
 * @returns
 */
export async function getUserDetail(data: {
  id: number;
  appCode: string;
  /** 当前访问系统标识 */
  userAppCode: string;
}) {
  return request<Response<IUserDetail>>('/user/account/detail', {
    method: 'post',
    data,
    prefix: URL_PREFIX_COMPATIBLE,
  });
}

/**
 * 用户状态
 * @param data
 * @returns
 */
export async function updateUserStatus(data: {
  id: number;
  userStatus: string;
  userAppCode: string;
}) {
  return request<Response<boolean>>('/user/account/change/user/status', {
    method: 'post',
    data,
    prefix: URL_PREFIX_COMPATIBLE,
  });
}

/**
 * 删除用户
 * @param data
 * @returns
 */
export async function postDelAppUser(data: { appCode: string; userCode: string | number }) {
  return request<Response<boolean>>('/user/account/del/app/user', {
    method: 'post',
    data,
    prefix: URL_PREFIX_COMPATIBLE,
  });
}
